using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;
using System.Text;

namespace BlazeApps.Library.Form
{
    [Serializable()]
    public class FormAnswerRowColumnDetail : Csla.BusinessBase<FormAnswerRowColumnDetail>
    {
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Business Properties and Methods

        //declare members
        private Guid _formResponseID = Guid.Empty;
        private Guid _formQuestionID = Guid.NewGuid();
        private Guid _formAnswerID = Guid.NewGuid();
        private Guid _formRowID = Guid.Empty;
        private Guid _formColumnID = Guid.Empty;
        private bool _isSelected = false;
		private string _otherText = String.Empty;
        private bool _hasData = false;

        public Guid FormResponseID
        {
            get
            {
                CanReadProperty("FormResponseID", true);
                return _formResponseID;
            }
            set
            {
                CanWriteProperty("FormResponseID", true);
                if (!_formResponseID.Equals(value))
                {
                    _formResponseID = value;
                    PropertyHasChanged("FormResponseID");
                }
            }
        }

        public Guid FormQuestionID
        {
            get
            {
                CanReadProperty("FormQuestionID", true);
                return _formQuestionID;
            }
            set
            {
                CanWriteProperty("FormQuestionID", true);
                if (!_formQuestionID.Equals(value))
                {
                    _formQuestionID = value;
                    PropertyHasChanged("FormQuestionID");
                }
            }
        }

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid FormAnswerID
        {
            get
            {
                CanReadProperty("FormAnswerID", true);
                return _formAnswerID;
            }
        }

        public Guid FormRowID
        {
            get
            {
                CanReadProperty("FormRowID", true);
                return _formRowID;
            }
            set
            {
                CanWriteProperty("FormRowID", true);
                if (!_formRowID.Equals(value))
                {
                    _formRowID = value;
                    PropertyHasChanged("FormRowID");
                }
            }
        }

        public Guid FormColumnID
        {
            get
            {
                CanReadProperty("FormColumnID", true);
                return _formColumnID;
            }
            set
            {
                CanWriteProperty("FormColumnID", true);
                if (!_formColumnID.Equals(value))
                {
                    _formColumnID = value;
                    PropertyHasChanged("FormColumnID");
                }
            }
        }

        public bool IsSelected
        {
            get
            {
                CanReadProperty("IsSelected", true);
                return _isSelected;
            }
            set
            {
                CanWriteProperty("IsSelected", true);
                if (!_isSelected.Equals(value))
                {
                    _isSelected = value;
                    PropertyHasChanged("IsSelected");
                }
            }
        }

		public string OtherText
		{
			get
			{
				CanReadProperty("OtherText", true);
				return _otherText;
			}
			set
			{
				CanWriteProperty("OtherText", true);
				if (value == null)
					value = string.Empty;
				if (!_otherText.Equals(value))
				{
					_otherText = value;
					PropertyHasChanged("OtherText");
				}
			}
		}

        public Boolean HasData
        {
            get
            {
                return _hasData;
            }
        }

        protected override object GetIdValue()
        {
            return _formAnswerID;
        }

        #endregion //Business Properties and Methods

        #region Validation Rules
        private void AddCustomRules()
        {
            //add custom/non-generated rules here...
        }

        private void AddCommonRules()
        {

        }

        protected override void AddBusinessRules()
        {
            AddCommonRules();
            AddCustomRules();
        }
        #endregion //Validation Rules

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in FormAnswerRowColumnDetail
            //AuthorizationRules.AllowRead("FormQuestionID", "FormAnswerRowColumnDetailReadGroup");
            //AuthorizationRules.AllowRead("FormAnswerID", "FormAnswerRowColumnDetailReadGroup");
            //AuthorizationRules.AllowRead("FormRowID", "FormAnswerRowColumnDetailReadGroup");
            //AuthorizationRules.AllowRead("FormColumnID", "FormAnswerRowColumnDetailReadGroup");
            //AuthorizationRules.AllowRead("IsSelected", "FormAnswerRowColumnDetailReadGroup");

            //AuthorizationRules.AllowWrite("IsSelected", "FormAnswerRowColumnDetailWriteGroup");
        }


        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in FormAnswerRowColumnDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("FormAnswerRowColumnDetailViewGroup"))
            //	return true;
            //return false;
        }

        public static bool CanAddObject()
        {
            //TODO: Define CanAddObject permission in FormAnswerRowColumnDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("FormAnswerRowColumnDetailAddGroup"))
            //	return true;
            //return false;
        }

        public static bool CanEditObject()
        {
            //TODO: Define CanEditObject permission in FormAnswerRowColumnDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("FormAnswerRowColumnDetailEditGroup"))
            //	return true;
            //return false;
        }

        public static bool CanDeleteObject()
        {
            //TODO: Define CanDeleteObject permission in FormAnswerRowColumnDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("FormAnswerRowColumnDetailDeleteGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private FormAnswerRowColumnDetail()
        { /* require use of factory method */
        }

        public static FormAnswerRowColumnDetail NewFormAnswerRowColumnDetail()
        {
            if (!CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a FormAnswerRowColumnDetail");
            return DataPortal.Create<FormAnswerRowColumnDetail>();
        }

        public static FormAnswerRowColumnDetail GetFormAnswerRowColumnDetail(Guid formAnswerID)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a FormAnswerRowColumnDetail");
            return DataPortal.Fetch<FormAnswerRowColumnDetail>(new Criteria(formAnswerID));
        }

        public static FormAnswerRowColumnDetail GetFormAnswerRowColumnDetail(Guid FormQuestionID, Guid FormResponseID, Guid FormRowID)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a FormAnswerRowColumnDetail");
            return DataPortal.Fetch<FormAnswerRowColumnDetail>(new Criteria(FormQuestionID, FormResponseID, FormRowID));
        }

        public static void DeleteFormAnswerRowColumnDetail(Guid formAnswerID)
        {
            if (!CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a FormAnswerRowColumnDetail");
            DataPortal.Delete(new Criteria(formAnswerID));
        }

        public override FormAnswerRowColumnDetail Save()
        {
            if (IsDeleted && !CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a FormAnswerRowColumnDetail");
            else if (IsNew && !CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a FormAnswerRowColumnDetail");
            else if (!CanEditObject())
                throw new System.Security.SecurityException("User not authorized to update a FormAnswerRowColumnDetail");

            return base.Save();
        }

        #endregion //Factory Methods

        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid FormAnswerID;

            public Criteria(Guid formAnswerID)
            {
                this.FormAnswerID = formAnswerID;
            }


            public Guid FormQuestionID = Guid.Empty;
            public Guid FormResponseID = Guid.Empty;
            public Guid FormRowID = Guid.Empty;

            public Criteria(Guid FormQuestionID, Guid FormResponseID, Guid FormRowID)
            {
                this.FormQuestionID = FormQuestionID;
                this.FormResponseID = FormResponseID;
                this.FormRowID = FormRowID;
            }
        }

        #endregion //Criteria

        #region Data Access - Create
        [RunLocal]
        protected override void DataPortal_Create()
        {
            ValidationRules.CheckRules();
        }

        #endregion //Data Access - Create

        #region Data Access - Fetch
        private void DataPortal_Fetch(Criteria criteria)
        {
            ExecuteFetch(criteria);
        }

        private void ExecuteFetch(Criteria criteria)
        {

            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            if (criteria.FormQuestionID != Guid.Empty && criteria.FormResponseID != Guid.Empty)
            {
                DAL.AddParameter("@FormQuestionID", criteria.FormQuestionID);
                DAL.AddParameter("@FormResponseID", criteria.FormResponseID);
                DAL.AddParameter("@FormRowID", criteria.FormRowID);

                using (SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetFormAnswerRowColumnByQuestionIDAndRowIDAndResponseID.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
                {
                    FetchObject(dr);
                    ValidationRules.CheckRules();

                    //load child object(s)
                    FetchChildren(dr);
                }
            }
            else
            {

                DAL.AddParameter("@FormAnswerID", criteria.FormAnswerID);

                using (SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetFormAnswerRowColumnByAnswerID.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
                {
                    FetchObject(dr);
                    ValidationRules.CheckRules();

                    //load child object(s)
                    FetchChildren(dr);
                }
            }
        }

        private void FetchObject(SafeDataReader dr)
        {
            if (dr.Read())
            {
                _formResponseID = dr.GetGuid("FormResponseID");
                _formQuestionID = dr.GetGuid("FormQuestionID");
                _formAnswerID = dr.GetGuid("FormAnswerID");
                _formRowID = dr.GetGuid("FormRowID");
                _formColumnID = dr.GetGuid("FormColumnID");
                _isSelected = dr.GetBoolean("IsSelected");
                _hasData = true;
            }
            else
            {
                _hasData = false;
            }
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch

        #region Data Access - Insert

        protected override void DataPortal_Insert()
        {
            ExecuteInsert();
        }

        private void ExecuteInsert()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddInsertParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.InsertFormAnswerRowColumn.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddInsertParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@FormResponseID", _formResponseID);
            DAL.AddParameter("@IsSelected", _isSelected);
            DAL.AddParameter("@FormQuestionID", _formQuestionID);
            DAL.AddParameter("@FormAnswerID", _formAnswerID);
            if (_formRowID == Guid.Empty)
            {
                DAL.AddParameter("@FormRowID", DBNull.Value);
            }
            else
            {
                DAL.AddParameter("@FormRowID", _formRowID);
            }

            if (_formColumnID == Guid.Empty)
            {
                DAL.AddParameter("@FormColumnID", DBNull.Value);
            }
            else
            {
                DAL.AddParameter("@FormColumnID", _formColumnID);
            }
			if (_otherText != string.Empty)
			{
				DAL.AddParameter("@OtherText", _otherText);
			}
			else
			{
				DAL.AddParameter("@OtherText", DBNull.Value);
			}
        }

        #endregion //Data Access - Insert

        #region Data Access - Update

        protected override void DataPortal_Update()
        {
            if (base.IsDirty)
            {
                ExecuteUpdate();
            }
        }

        private void ExecuteUpdate()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddUpdateParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.UpdateFormAnswerRowColumn.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddUpdateParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@FormResponseID", _formResponseID);
			DAL.AddParameter("@IsSelected", _isSelected);
            DAL.AddParameter("@FormQuestionID", _formQuestionID);
            DAL.AddParameter("@FormAnswerID", _formAnswerID);
			if (_formRowID == Guid.Empty)
			{
				DAL.AddParameter("@FormRowID", DBNull.Value);
			}
			else
			{
				DAL.AddParameter("@FormRowID", _formRowID);
			}

			if (_formColumnID == Guid.Empty)
			{
				DAL.AddParameter("@FormColumnID", DBNull.Value);
			}
			else
			{
				DAL.AddParameter("@FormColumnID", _formColumnID);
			}
			if (_otherText != string.Empty)
			{
				DAL.AddParameter("@OtherText", _otherText);
			}
			else
			{
				DAL.AddParameter("@OtherText", DBNull.Value);
			}
        }

        private void UpdateChildren(SqlConnection cn)
        {
        }
        #endregion //Data Access - Update

        #region Data Access - Delete
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new Criteria(_formAnswerID));
        }

        private void DataPortal_Delete(Criteria criteria)
        {
            ExecuteDelete(criteria);
        }

        private void ExecuteDelete(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddDeleteParameters(ref DAL, criteria);

            DAL.ExecuteNonQuery(Procs.DeleteFormAnswerRowColumn.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddDeleteParameters(ref BA.DAL.DBUtility DAL, Criteria criteria)
        {
            DAL.AddParameter("@FormAnswerID", DbType.Guid, criteria.FormAnswerID);
        }

        #endregion //Data Access - Delete
        #endregion //Data Access
    }
}